package com.cdzs.twocamera;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.cdzs.twocamera.constant.Constant;
import com.cdzs.twocamera.myinterface.OnReceiveCallbackBlock;
import com.cdzs.twocamera.myinterface.OnServerConnectedCallbackBlock;
import com.cdzs.twocamera.myinterface.OnServerDisconnectedCallbackBlock;
import com.cdzs.twocamera.tcp.Client;
import com.cdzs.twocamera.tcp.TCPClient;
import com.cdzs.twocamera.util.ArrayMergeUtil;
import com.cdzs.twocamera.util.NumberUtil;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.nio.ByteOrder;
import java.util.Timer;
import java.util.TimerTask;

public class TestActivity extends AppCompatActivity {

    private static final String TAG = "TestActivity";

    private TCPClient mTCPClient;
    private TimerTask task;
    private final Timer timer = new Timer();
    private StringBuilder stringBuilder = new StringBuilder();
    private byte[] allBytes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        //sendTCP();
    }

    /**
     * 发送心跳包
     * @param view
     */
    public void click(View view) {

        if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
            Log.d(TAG, "click: 大端");
        } else {
            Log.d(TAG, "click: 小端");
        }

        // 发送心跳包
        Client client = new Client(Constant.SERVER_IP, Constant.SERVER_PORT);
        client.start();
    }

    public void sendTCP() {

        TCPClient.getInstance().setDisconnectedCallback(new OnServerDisconnectedCallbackBlock() {
            @Override
            public void serverDisconnectedCallback(IOException e) {
                Log.d(TAG, "serverDisconnectedCallback: 断开连接");
            }
        });
        TCPClient.getInstance().setConnectedCallback(new OnServerConnectedCallbackBlock() {
            @Override
            public void serverConnectedCallback() {
                Log.d(TAG, "serverConnectedCallback: 连接成功");
            }
        });
        TCPClient.getInstance().setReceivedCallback(new OnReceiveCallbackBlock() {
            @Override
            public void callback(byte[] receivedData) {
                // 处理收到的数据
//                stringBuilder.append(new String(receivedData, Charset.forName("UTF-8")) + "收到数据");
                stringBuilder.append("收到数据");

                sendMessageMainThread(stringBuilder.toString());
            }

        });

        //连接
        TCPClient.getInstance().connect();

        //请求头、数据长度、
        allBytes = ArrayMergeUtil.byteMergerAll(
                NumberUtil.getByteArray(Constant.NET_HEADER_DATA),
                NumberUtil.getByteArray(28),
                NumberUtil.getByteArray(Constant.HEART_BEAT_PACKAGE_TYPE),
                NumberUtil.getByteArray(Constant.HEART_BEAT_PACKAGE_DATA),
                NumberUtil.getByteArray(Constant.HEART_BEAT_PACKAGE_DATA),
                NumberUtil.getByteArray(Constant.HEART_BEAT_PACKAGE_DATA),
                NumberUtil.getByteArray(Constant.HEART_BEAT_PACKAGE_DATA)
        );

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //发送
                TCPClient.getInstance().sendEasy(allBytes);
            }
        }, 0, 3000);


        //断开连接
//        TCPClient.getInstance().disconnect();


//        timer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                sendHeartBeatPackage();
//            }
//        },0,3000);

    }

    /**
     * 发送心跳包,位置 tcp 连接
     */
    public void sendHeartBeatPackage() {

        // 创建套接字，连接服务器
        mTCPClient = TCPClient.getInstance();

//        mTCPClient.setOnProcessTcpDataImp(new ProcessTcpDataImp() {
//            @Override
//            public void processData(byte[] data) {
//                sendMessageMainThread(new String(data, Charset.forName("UTF-8")) + "收到数据");
//            }
//        });

        //mTCPClient.connect(Constant.SERVER_IP, Constant.SERVER_PORT);
//        mTCPClient.connect("172.16.7.49", 7777);

        //请求头、数据长度、
        byte[] allBytes = ArrayMergeUtil.byteMergerAll(
                NumberUtil.getByteArray(Constant.NET_HEADER_DATA),
                NumberUtil.getByteArray(28),
                NumberUtil.getByteArray(Constant.HEART_BEAT_PACKAGE_TYPE),
                NumberUtil.getByteArray(Constant.HEART_BEAT_PACKAGE_DATA),
                NumberUtil.getByteArray(Constant.HEART_BEAT_PACKAGE_DATA),
                NumberUtil.getByteArray(Constant.HEART_BEAT_PACKAGE_DATA),
                NumberUtil.getByteArray(Constant.HEART_BEAT_PACKAGE_DATA)
        );

        mTCPClient.send(allBytes);
    }

    private void sendMessageMainThread(String msg) {
        Message message = Message.obtain();
        message.obj = msg + "\n";
        mHandler.sendMessage(message);
    }

    private TestActivity.MyHandler mHandler = new TestActivity.MyHandler(this);



    private static class MyHandler extends Handler {

        private WeakReference<TestActivity> refActivity;

        MyHandler(TestActivity activity) {
            refActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            TestActivity activity = refActivity.get();
            Log.d(TAG, "handleMessage: " + msg.obj);
            //activity.tv_msg.append((CharSequence) msg.obj);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
